package com.cloud.admin.controller;

import com.cloud.admin.beans.vo.DictListVO;
import com.cloud.common.util.base.Result;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.admin.beans.po.SysDictList;
import com.cloud.admin.service.SysDictListService;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.cloud.common.data.util.ObjUtil;

import javax.validation.Valid;

/**
 * 字典项list
 *
 * @author Aijm
 * @date 2019-09-05 19:52:37
 */
@RestController
@RequestMapping("/sysdictlist" )
@Api(value = "sysdictlist", tags = "sysdictlist管理")
public class SysDictListController {

    @Autowired
    private SysDictListService sysDictListService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param sysDictList 字典项list
     * @return
     */
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('admin_sysdictlist_view')")
    public Result getSysDictListPage(Page page, SysDictList sysDictList) {
        return Result.success(sysDictListService.page(page, Wrappers.query(sysDictList)));
    }


    /**
     * 通过id查询字典项list
     * @param id id
     * @return Result
     */
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('admin_sysdictlist_view')")
    public Result getById(@PathVariable("id") Long id) {
        return Result.success(sysDictListService.getById(id));
    }

    /**
     * 通过typecode查询字典项list
     * @param typeCode typeCode
     * @return Result
     */
    @GetMapping("/type/{typeCode}")
    @PreAuthorize("@pms.hasPermission('admin_sysdictlist_view')")
    public Result getByTypeCode(@PathVariable("typeCode") String typeCode) {
        return Result.success(DictListVO.builder()
                .dictLists(sysDictListService.getDictListByType(typeCode)).build());
    }

    /**
     * 新增字典项list
     * @param sysDictList 字典项list
     * @return Result
     */
    @PostMapping
    @PreAuthorize("@pms.hasPermission('admin_sysdictlist_add')")
    public Result save(@RequestBody @Valid SysDictList sysDictList) {
        return Result.success(sysDictListService.save(sysDictList));
    }

    /**
     * 修改字典项list
     * @param sysDictList 字典项list
     * @return Result
     */
    @PutMapping
    @PreAuthorize("@pms.hasPermission('admin_sysdictlist_edit')")
    public Result updateById(@RequestBody @Valid SysDictList sysDictList) {
        return Result.success(sysDictListService.updateById(sysDictList));
    }

    /**
     * 通过id删除字典项list
     * @param id id
     * @return Result
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('admin_sysdictlist_del')")
    public Result removeById(@PathVariable Long id) {
        SysDictList byId = sysDictListService.getById(id);
        return Result.success(sysDictListService.removeByDictList(byId));
    }

}
